# This is a_ sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import numpy as np

num: int = 0


def runTest(fun):
    global num
    print('----------------------', num)
    fun()
    num += 1
    print()


def print_hi(name):
    # Use a_ breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


def ndTest():
    a = np.array([1, 2, 3])  # 使用列表构建一维数组
    print(a)
    # ndarray数组类型
    print(type(a))

    b = np.array([[1, 2, 3], [4, 5, 6]])
    print(b)

    c = np.array([2, 4, 6, 8], dtype="complex")
    print(c)
    print(c[0] * c[1])
    print(type(c[0]))

    arr = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [9, 10, 11, 23]])
    print(arr.ndim)


def npReshape():
    e = np.array([[1, 2], [3, 4], [5, 6]])
    print("原数组\n", e)
    e = e.reshape(2, 3)
    print("新数组\n", e)


def npType():
    a = np.dtype(np.int64)
    print(a)


# ---------------------------------------- np data type
def npData():
    dt = np.dtype([('score', 'i1')])
    print(dt)
    # 定义字段名score，以及数组数据类型i1
    dt = np.dtype([('score', 'i1')])
    a = np.array([(55,), (75,), (85,)], dtype=dt)
    print(a)
    print(a.dtype)
    print(a['score'])
    print()
    #  S 表示 byte str
    teacher = np.dtype([('name', 'S20'), ('age', 'i1'), ('salary', 'f4')])
    # 输出结构化数据teacher
    print(teacher)
    # 将其应用于ndarray对象
    b = np.array([('ycs', 32, 6357.50), ('jxe', 28, 6856.80), (33, 22, 11)], dtype=teacher)
    print(b)
    print(b['name'])


def npReshape2():
    a = np.array([[2, 4, 6], [3, 5, 7]])
    print(a.shape)

    print()
    a = np.array([[1, 2, 3], [4, 5, 6]])
    a.shape = (3, 2)
    print(a)

    print()
    a = np.array([[1, 2, 3], [4, 5, 6]])
    b = a.reshape(3, 2)
    print(b)


def ndArrDim():
    # 随机生成一个一维数组
    c = np.arange(24)
    print(c)
    print(c.ndim)
    # 对数组进行变维操作
    e = c.reshape(2, 4, 3)
    print(e)
    print(e.ndim)


def ndArrSize():
    x = np.array([1, 2, 3, 4, 5], dtype=np.int8)
    print(x.itemsize)

    x = np.array([1, 2, 3, 4, 5], dtype=np.int64)
    print(x.itemsize)


def ndArrFlags():
    x = np.array([1, 2, 3, 4, 5])
    print(x.flags)

    print()
    y = np.array([[1, 2, 3, 4, 5], [6, 7, 8]], dtype=object)
    print(y.shape)
    print(y.flags)


def ndArrInit():
    arr = np.empty((3, 2), dtype=int)
    print(arr)

    print()
    # 默认数据类型为浮点数
    a = np.zeros(6, order="C")
    print(a)
    b = np.zeros(6, dtype="complex64", order="C")
    print(b)

    print()
    c = np.zeros((3, 3), dtype=[('x', 'i4'), ('y', 'i4')])
    print(c)
    # 输出x,y，并指定的数据类型


def ndArrasarray():
    l = [1, 2, 3, 4, 5, 6, 7]
    a = np.asarray(l);
    print(type(a))
    print(a)

    print()
    l = (1, 2, 3, 4, 5, 6, 7)
    a = np.asarray(l);
    print(type(a))
    print(a)

    print()
    l = [[1, 2, 3, 4, 5, 6, 7], [8, 9]]
    a = np.asarray(l, dtype=object);
    print(type(a))
    print(a)


def ndArrFrombuffer():
    # 字节串类型
    l = b'hello world'
    print(type(l))
    a = np.frombuffer(l, dtype="S1")
    print(a)
    print(type(a))


def ndArrFromiter():
    # 使用 range 函数创建列表对象
    miter = range(6)
    print(miter)
    # 使用i迭代器，通过fromiter方法创建ndarray
    array = np.fromiter(miter, dtype=float)
    print(array)


def ndArrArange():
    x = np.arange(8)
    print(x)

    print()
    x = np.arange(1, 10, 2)
    print(x)


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    print_hi('PyCharm')
    runTest(ndTest)
    runTest(npReshape)
    runTest(npType)
    runTest(npData)
    runTest(npReshape2)
    runTest(ndArrDim)
    runTest(ndArrSize)
    runTest(ndArrFlags)
    runTest(ndArrInit)
    runTest(ndArrasarray)
    runTest(ndArrFrombuffer)
    runTest(ndArrFromiter)
    print()
    runTest(ndArrArange)
